<?php
namespace App\Repositories;
use App\Exceptions\BasicException;
use App\Models\GameDB\Fantasy\DisplayCate;

//fantasy 许愿魔法书 补池处理逻辑  2024.5.20

class AddPoolFantasyRepository extends AddPoolRepository
{
    public function __construct()
    {
        parent::__construct();
        //通用
        $this->game_type = 2;//数据库字段废弃，为避免报错将值设置为2
        $this->is_add_pool = app('model_config')->getValue('IS_ADD_POOL');

        //分类开始
        $this->set_type = 'fantasy';
        $this->poolUserModel = app('model_fantasy_pool_user');
        $this->poolGroupModel = app('model_fantasy_pool_group');
        $this->poolPrizeModel = app('model_fantasy_pool_prize');
        $this->poolConfigModel = app('model_fantasy_pool_config');
        $this->checkModel = app('model_fantasy_draw_check');
        $this->checkRecordModel = app('model_fantasy_draw_check_record');
        $this->giftConfigModel = app('model_fantasy_gift_config');
        $this->giftTemplateModel = app('model_fantasy_gift_template');
        $this->addPoolConfigModel = app('model_fantasy_draw_add_pool_config');
        $this->addPoolAccountModel = app('model_fantasy_draw_add_pool_account');
        $this->addPoolAccountRecordModel = app('model_fantasy_draw_add_pool_account_record');
        $this->appointGiftModel = app('model_fantasy_appoint_gift');
    }

    //展示区动画分类
    public function getDisplayCate($params = [])
    {
        /*$prePage = $params['prePage'] ?? 20;
        $list = DisplayCate::query()
            ->orderBy('id', 'desc')->paginate($prePage)->toArray();
        return $list;*/

        $redis = $this->redis;
        $redis_key = $this->set_type.':display_cate';
        if ($redis->exists($redis_key)) {
            $list = json_decode($redis->get($redis_key), true);
        } else {
            $list = DisplayCate::query()
                ->orderBy('check_money', 'desc')->get()->toArray();

            foreach ($list as $k => &$v) {
                $v['display_level'] = $k + 1;
            }
            if (!empty($list)) {
                $redis->set($redis_key, json_encode($list, JSON_UNESCAPED_UNICODE));
            }
        }
        return $list;
    }

    public function setDisplayCate($params)
    {
        $data = [
            'check_money'  => $params['check_money'],
            'rate'         => $params['rate'],
        ];
        if ($params['rate'] <= 0 || $params['check_money'] <= 0) {
            throw new BasicException('-1', '不能为空或小于0');
        }
        if ($params['id']) {
            DisplayCate::query()->where('id', $params['id'])->update($data);
        } else {
            DisplayCate::query()->insert($data);
        }
        $redis = $this->redis;
        $redis_key = $this->set_type.':display_cate';
        $redis->del($redis_key);
        return true;
    }

    public function displayCateDel($id)
    {
        $redis = $this->redis;
        $redis_key = $this->set_type.':display_cate';
        $redis->del($redis_key);
        return DisplayCate::query()->where('id', $id)->delete();
    }

    public function getMonsterDisplay($gift_list, $userId)
    {
        $redis = $this->redis;
        $display_cate_list = $this->getDisplayCate();
        $end = end($display_cate_list);
        $redis_key = $this->set_type.':monster_hp';
        $redis_expire_key = $this->set_type.':monster_expire:'.$userId;

        $hp_status = true;
        foreach ($gift_list as &$gift) {
            $gift['display_level'] = $end['display_level'];
            if ($gift['price'] < $end['check_money']) {
                continue;
            }
            foreach ($display_cate_list as $display) {
                if ($gift['price'] >= $display['check_money']) {
                    $gift['display_level'] = $display['display_level'];
                    if ($hp_status && !$redis->exists($redis_expire_key)) {
                        $hp_status = false;
                        $monster_hp = $redis->hincrby($redis_key, $userId, 0 - $display['rate']);
                        if ($monster_hp <= 0) {
                            $redis->set($redis_expire_key, 1, 'ex', 2, 'nx');
                            $redis->hset($redis_key, $userId, 100);
                        }
                    }
                    break;
                }
            }
        }
        return $gift_list;
    }

    public function getMonsterHp($userId)
    {
        $redis = $this->redis;
        $redis_key = $this->set_type.':monster_hp';
        $redis_expire_key = $this->set_type.':monster_expire:'.$userId;
        if ($redis->exists($redis_expire_key)) {
            $monster_hp = '0';
        } else {
            if ($redis->hexists($redis_key, $userId)) {
                $monster_hp = $redis->hget($redis_key, $userId);
            } else {
                $monster_hp = '100';
                $redis->hset($redis_key, $userId, $monster_hp);
            }
        }
        return $monster_hp;
    }



}
